//
//  GoodsCollectionCtr.swift
//  eemall
//
//  Created by zhuxietong on 2018/9/13.
//  Copyright © 2018年 zhuxietong. All rights reserved.
//

import Foundation

import AsyncDisplayKit
import UIKit
import soapp
import Eelay
import JoLoading
import WebKit

// import mosdks

extension GoodsCollectionCtr{
    
    open class Cell: JoCellNode {
        public let saveNumL = JoTextNode()
        public let saveEditBt = ASButtonNode()
        public var saveActionBar:_Spec
        {
            get{
                return .stack(["h|6",&-.center,_grow(1),_shrink(1),_justfy(.space_between)], childs: [
                    saveNumL.Spec,
                    saveEditBt.Spec
                    ])
            }
        }
        
        public let imgV = JoImageNode()
        public let titleL = ASTextNode()
        public let priceL = ASTextNode()
        
        
        
        
        open override func addLayoutRules() {
            self.backgroundColor = ._cellBackground
            separatorHeight = TP.separatorHeight
            titleL.maximumNumberOfLines = 1
            saveEditBt.addTarget(self, action: #selector(deleteAction), forControlEvents: .touchUpInside)
            internalCellTouch = true
            separatorColor = UIColor._separator
            separatorHeight = TP.separatorHeight
            imgV.isUserInteractionEnabled = false
        }
        
        open override func cellTouch() {
            Goods.seeDetail(obj: mo.model, at: content_controller)
        }
        
        open override func specGetter() -> _Spec {
            return .inset([],[10,16], child:.stack(["h|10",_shrink(1),&-.stretch,_nowrap], childs: [
                imgV["\(88)",88],
                .stack(["v|3",_shrink(1),_justfy(.space_between),&-.stretch,_grow(1)], childs: [
                    titleL[_shrink(1),_grow(1)],
                    priceL[_shrink(1),_grow(1)],
                    saveActionBar
                    ])
                ]))
        }
        
        open override func mo_model_reload() {
            
            imgV.img_url = mo.model["thumb",""]
            //            imgV.url = URL(string: mo.model["thumb",""])
            //            imgV.url = URL(string:"http://img4.duitang.com/uploads/blog/201409/06/20140906181244_WAjKB.gif")
            titleL.attributedText = mo.model["title",""].ui.color(color: ._foreground).font13.lineSpace(3).astring
            let price = mo.model[float:"marketprice",0].price_display
            priceL.attributedText = "¥".ui.font10.color(color: ___shop.second).astring + price.ui.color(color: ___shop.second).font15.astring
            
            let saveStr = "[XX]人收藏".replaceVars(values: mo.model["collect_count","0"])
            saveNumL.attributedText = saveStr.ui.color(color: ._foregroundWeak).font11.lineSpace(5).astring
            
            saveEditBt.ui.font(size: 12).ncolor(color: .color(light: "#666", dark: "#ccc")).ntitle(title: "删除".loc)
            separatorColor = UIColor._separator
            separatorHeight = TP.separatorHeight
        }
        
        @objc func deleteAction() {
            weak var wself = self
            JoAlertView(info: "提示".loc,"是否确认移出该收藏商品".loc).append(title: "取消".loc, style: .destructive, action: {
                
            }).append(title: "移除".loc) {
                wself?.canleRequest()
                }.alert.show(at: content_controller)
        }
        
        func canleRequest() {
            weak var wself = self
            let model = self.mo.model
            HUD.share.show().mode(.indeterminate)
            JoTask().post._surl(.商品收藏toggle).params(["id":model["id",""]]).json_handle { (status, msg, obj, resp) in
                HUD.share.hide(animated: true)
                if status.isSuccess{

                    if let ctr = wself?.content_controller as? GoodsCollectionCtr{
//                        wself?.indexPathDelete()
                        ctr.reload_page()
                    }
                    "已取消收藏".loc.alert()
                    return
                }
                }.run()
        }
        
    }
    
    
    
}

open class GoodsCollectionCtr:ASCollectionContainer{
    public static var CellType:JoCellNode.Type = Cell.self
    
    
    override open func viewDidLoad() {
        //        self.page_load_section = "section1"
        self.content = ASTermsContainer.list(
            net: JoTask().post._surl(.我收藏的商品),
            node: "list",
            emptyMsg: "没有相关内容".loc,
            params: nil,
            cellType: Cell.self,
            detailType: Goods.DetailCtrType,
            test: false
        )
        
        super.viewDidLoad()
        self.title = "商品收藏".loc
        //        paginator.parameters = ["id":mo.model["id",""]]
        
        collectionNode.view.backgroundView?.backgroundColor = UIColor._background
        collectionNode.backgroundColor = UIColor._background
        
        if let _ = JoUser.loginUser()
        {
            reload_page()
        }
        else{
           showUnlogin()
        }
        
        
        __notice.observer(JoUser.Notice.login,JoUser.Notice.logout) {[weak self] (type, notice) in
            
            switch type.name{
            case JoUser.Notice.login.name:
                self?.loadingV.dismiss()
                self?.reload_page()
            case JoUser.Notice.logout.name:
                self?.showUnlogin()
            default:
                break
            }
        }
        
    }
    
    func doLogin() {
        EeAuthControl.getUser { (success, msg, user) in
            
        }
    }
    
    func showUnlogin() {
        weak var wself = self
        self.loadingV.handle(message: "登录用户查看您收藏的商品".loc, title: "", button: "立即登录".loc, handAction: {
            wself?.doLogin()
            
        })
    }
}


